package com.huawei.crm.web.domain;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 列表数据结果集
 *
 * @param <T>
 * @author luohua
 * @date 2021年12月30日17:18:50
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ListResult<T> extends BaseResult {

    private static final long serialVersionUID = -6918271562879376318L;

    private List<T> data;

    public ListResult(List<T> data) {
        super(ResultCode.SUCCESS);
        this.data = data;
    }

    public ListResult(int code, String message) {
        super(code, message);
    }

    public ListResult(int code, String message, List<T> data) {
        super(code, message);
        this.data = data;
    }

    public ListResult<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ListResult<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public ListResult<T> data(List<T> data) {
        this.setData(data);
        return this;
    }

    public static <T> ListResult<T> of(ResultCode status) {
        return new ListResult<>(status.code(), status.message());
    }

    public static <T> ListResult<T> of(ResultCode status, String message) {
        return new ListResult<>(status.code(), message);
    }

    public static <T> ListResult<T> of(ResultCode status, List<T> data) {
        return new ListResult<>(status.code(), status.message(), data);
    }

    public static <T> ListResult<T> of(ResultCode status, String message, List<T> data) {
        return new ListResult<>(status.code(), message, data);
    }

    public static <T> ListResult<T> success() {
        return new ListResult<>(ResultCode.SUCCESS.code(), ResultCode.SUCCESS.message());
    }

    public static <T> ListResult<T> success(List<T> data) {
        return new ListResult<>(ResultCode.SUCCESS.code(), ResultCode.SUCCESS.message(), data);
    }

    public static <T> ListResult<T> error() {
        return new ListResult<>(ResultCode.FAILED.code(), ResultCode.FAILED.message());
    }

    public static <T> ListResult<T> error(String message) {
        return new ListResult<>(ResultCode.FAILED.code(), message);
    }

    public static <T> ListResult<T> error(Integer code, String message) {
        return new ListResult<>(code, message);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
